package SingleThread;
import java.util.Random;

import MonteCarloGUI.MonteCarloGUI;

public class MonteCarlo {
	String CallPutFlag;
	double S, X, T, r, b, v;
	int nSteps, nSimulations;
	double dt, St, Sum, Drift, vSqrdt;
	
	public MonteCarlo(double S, double X, double r, double T, double b, double v, int Steps, int Simul, String PC) {
		this.S=S; 
		this.X=X; 
		this.r=r; 
		this.T=T; 
		this.b=b; 
		this.v=v; 
		this.nSteps=Steps; 
		this.nSimulations=Simul;
		this.CallPutFlag=PC;
	}
	public double calcul() {
	    //final long start = System.nanoTime();
		Random rand = new Random();
		dt = T/nSteps;
		Drift = (b - (Math.pow(v,2) / 2)) * dt; 
		vSqrdt = v * Math.sqrt(dt); 
		int z = 1;
		if (CallPutFlag == "c") {
			z=1;
		}
		if (CallPutFlag == "p") {
			z=-1;
		}
		Sum=0;
		for (int i=0; i<nSimulations; i++) {
			St=S;
			for (int j=0; j<nSteps; j++) {
			 St=St*Math.exp(Drift+vSqrdt*rand.nextGaussian());
			 }
			//System.out.println(z*(St-X));
			Sum=Sum+Math.max(z*(St-X), 0);
		}
		double result=Math.exp(-r*T)*Sum/nSimulations;
		//System.out.println(result);
	    //final long end = System.nanoTime();
	    //System.out.println("Time (seconds) taken " + (end - start)/1.0e9);
		return result;
		
	}
	
	public static void main(String args[]) {
		MonteCarloGUI MtCarloG = new MonteCarloGUI();
	}
}